<?php

namespace App\Logics;

use App\Models\Event;
use App\Models\EventPropose;
use App\Models\Order;
use App\Models\SiteConfig;
use App\Models\User;
use App\Models\UserMoney;
use App\Service\Base\Logic;

class UserPay extends Logic
{

    // 创建：用户支付
    public function add($myUid)
    {
        $opt      = $this->option();
        $order_id = $opt->getData('order_id', 0, 'intval');
        $orderDb  = new Order();
        if(empty($order_id)){
            return $this->_returnFail('请指定支付订单编号！');
        }

        $order = $orderDb->isCache(true)->oneFind(['id' => $order_id]);
        if(empty($order)){
            return $this->_returnFail('请指定支付订单不存在！');
        }


        $data = [
            'uid'         => $myUid,
            'auth_id'     => $opt->getData('auth_id', 0, 'intval'),
            'order_id'    => $opt->getData('order_id', 0, 'intval'),
            'trade_no'    => $opt->getData('trade_no', null, 'trim'),
            'money'       => $opt->getData('money', 0, 'intval'),
            'description' => $opt->getData('description', null, 'trim'),
            'type'        => $myUid,
        ];

        $res = $orderDb->oneAdd($data);
        if($res === false){
            return $this->_returnFail($orderDb->getError());
        }

        return $res;
    }

    // 用户支付 并计算收益
    //   支付返回的时候进行的操作
    public function jisuanMoney($pay_id)
    {
        // 支付状态
        $payDb   = new  \App\Models\UserPay();
        $payInfo = $payDb->isCache(true)->oneFind(['id' => $pay_id]);
        if(empty($payInfo)){
            return $this->_returnFail('支付的订单不存在！');
        }
        if($payInfo['status'] == 1){
            return $this->_returnFail('支付的订单已经处理成功！');
        }
        if($payInfo['status'] == 2){
            return $this->_returnFail('支付的订单已经处理失败！');
        }

        // 查询订单状态
        $payUid   = $payInfo['status'];
        $payMoney = $payInfo['money'];
        $currTime = time();
        $allMoney = [];
        if(empty($payInfo['auth_id'])){
            // 查询订单  查询报名活动
            $orderDb   = new Order();
            $orderInfo = $orderDb->isCache(true)->oneFind(['id' => $payInfo['order_id']]);
            if(empty($orderInfo)){
                return $this->_returnFail('支付的订单不存在！');
            }

            // 查询系统配置
            $config = (new SiteConfig())->getConfig();
            $config = array_column($config, 'value', 'field');
            $_rate  = $config['platform_rate'] ?? 15;

            // 参数
            $eventId   = $orderInfo['event_id'];
            $payMoney  = $orderInfo['total'];
            $userMoney = $payMoney;
            $eventInfo = (new Event())->isCache(true)->oneFind(['id' => $eventId]);
            if(empty($eventInfo)){
                return $this->_returnFail('支付的订单活动不存在！');
            }
            // 计算收益
            // 计算收益1：计算平台收益
            $userMoney = $userMoney - $userMoney * $_rate;
            // 计算收益2：计算代理商收益
            $allMoney[] = $this->jisuanMoneyAgent($payUid, $payMoney, 'order');
            // 计算收益3：计算推荐收益
            // 查询推荐
            $proposeInfo = (new EventPropose())->oneFind(['to_uid' => $payUid]);
            if($proposeInfo){
                $_getMoney  = $orderInfo['total'] - $payInfo['money'];
                $allMoney[] = ['uid' => $proposeInfo['uid'], 'form_uid' => $payUid, 'total' => $payMoney, 'type' => 'order', 'description' => '推荐用户报名提成', 'money' => $_getMoney, 'ctime' => $currTime, 'mtime' => $currTime,];
                $userMoney  = $userMoney - $_getMoney;
            }

            // 计算收益4：计算发布者的收益
            $allMoney[] = ['uid' => $eventInfo['uid'], 'form_uid' => $payUid, 'total' => $payMoney, 'type' => 'signup', 'description' => '发布活动报名费用', 'money' => $userMoney, 'ctime' => $currTime, 'mtime' => $currTime,];

        } else{
            // 计算收益
            // 计算收益1：计算平台收益
            // 计算收益2：计算代理商收益
            $allMoney[] = $this->jisuanMoneyAgent($payUid, $payMoney, 'auth');
        }

        // 保存收益信息
        $res = (new UserMoney())->insert($allMoney);

        return $res;
    }

    #################################################################################################################
    // 计算收益1：计算平台收益
    public function jisuanMoneyPlatform($payUid, $payMoney, $type = 'auth')
    {
        // 1、查询代理商收入比例
        $config = (new SiteConfig())->getConfig();
        $config = array_column($config, 'value', 'field');
        $_rate  = $config['platform_rate'] ?? 15;

        // 2、计算代理商收入，返回代理商收入信息
        $_get  = $payMoney * $_rate / 100;
        $_desc = $type == 'auth' ? '推荐用户认证提成' : '用户报名提成';
        $money = ['uid' => 0, 'form_uid' => $payUid, 'total' => $payMoney, 'description' => $_desc, 'money' => $_get,];

        return $money;
    }

    // 计算收益2：计算代理商收益
    public function jisuanMoneyAgent($payUid, $payMoney, $type = 'auth')
    {
        // 1、查询代理商
        $userInfo = (new User())->isCache(true)->oneFind(['id' => $payUid]);
        if(empty($userInfo['from_uid'])){
            return [];
        }

        // 2、查询代理商收入比例
        $config = (new SiteConfig())->getConfig();
        $config = array_column($config, 'value', 'field');
        $_rate  = $config['agent_rate'] ?? 0;
        $time   = time();

        // 3、计算代理商收入，返回代理商收入信息
        $_get  = $payMoney * $_rate / 100;
        $_from = $userInfo['from_uid'];
        $_desc = $type == 'auth' ? '推荐用户认证提成' : '用户报名提成';
        $money = ['uid' => $_from, 'form_uid' => $payUid, 'total' => $payMoney, 'type' => $type, 'description' => $_desc, 'money' => $_get, 'ctime' => $time, 'mtime' => $time,];

        return $money;
    }

    // 计算收益3：计算推荐收益


    //

    // 订单状态
    //  订单退款


}


